﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Alquiler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    String SQL, mssg;
    AdmiDatos.Service ad = new AdmiDatos.Service();
    System.Data.DataSet dst = new System.Data.DataSet();
    Formato1 vt = new Formato1();

    private void habilitarExiste()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = true;
        BtnEliminar.Enabled = true;
    }
    private void habilitarNuevo()
    {
        BtnAgregar.Enabled = true;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
    }
    private void deshabilitarBotones()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
        vt.LimpiartextBoxes(this);
    }

    protected void BtnAgregar_Click(object sender, EventArgs e)//no agrega
    {
        try
        {
            string fechaE = DatePicker1.SelectedDate.ToString();
            string fechaI = DatePicker2.SelectedDate.ToString();
            string lista = ListaEstado.SelectedItem.ToString();
            SQL = "Insert into Alquiler(PeliId,FechaInicio,FechaEntrega,PrecioDiario,MontoAlquiler,Estado,UsuarioId,MontoMulta)";
            SQL += "Values('" + TxtIdpeli.Text + "','" + fechaI + "',' " + fechaE  + "',' " + TxtPrecioD.Text + "','" + TxtMontoA.Text + "','";
            SQL += lista + "','" + TxtIdUsuario.Text + "','" + TxtMontoMulta.Text + "')";
            Response.Write(SQL);
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("Los Registros han sido ingresado exitosamente!", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }

    }

    protected void BtnModificar_Click(object sender, EventArgs e)
    {
        try
        {
            String fechaE = DatePicker1.SelectedDate.ToString();
            SQL = "Update Alquiler set FechaEntrega ='" + fechaE + "'";
            SQL += ",PrecioDiario ='" + TxtPrecioD.Text + "'";
            SQL += ",MontoAlquiler = '" + TxtMontoA.Text + "'";
            SQL += ",Estado = '" + ListaEstado.SelectedItem.ToString() + "'";
            SQL += ",UsuarioId = '" + TxtIdUsuario.Text + "'";
            SQL += ",MontoMulta = '" + TxtMontoMulta.Text + "'";
            SQL += "where PeliId  = '" +  TxtIdUsuario.Text + "'"; 
            Response.Write(SQL);
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido modificado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }

    }
    protected void BtnEliminar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Delete from Alquiler Where PeliId = '" + TxtIdpeli.Text + "'";
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido eliminado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            SQL = "Select * From Alquiler Where PeliId = '" + TxtIdpeli.Text + "'";
            ad.ObtenerD(ref mssg, SQL, ref dst);
            if (dst.Tables[0].Rows.Count > 0)
            {
                TxtPrecioD.Text = dst.Tables[0].Rows[0][3].ToString();
                TxtMontoA.Text = dst.Tables[0].Rows[0][4].ToString();
                //ListaEstado.SelectedIndex.ToString() = dst.Tables[0].Rows[0][5].ToString();
                TxtIdUsuario.Text = dst.Tables[0].Rows[0][6].ToString();
                TxtMontoMulta.Text = dst.Tables[0].Rows[0][7].ToString();
                DatePicker2.SelectedDate = Convert.ToDateTime(dst.Tables[0].Rows[0][1].ToString());
                DatePicker1.SelectedDate = Convert.ToDateTime(dst.Tables[0].Rows[0][2].ToString());
                habilitarExiste();
            }
            else
            {
                vt.MessageBox("Registro no encontrado!", this);
                habilitarNuevo();
                Session["PeliId"] = TxtIdpeli.Text;
                vt.LimpiartextBoxes(this);
                TxtIdpeli.Text = Session["PeliId"].ToString();
            }
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
}